import React, { Component } from 'react'
import "../assets/css/index.css"

//  图片
import logo from "../assets/image/logo.png"
import yh from "../assets/image/yh.png"
import yh2 from "../assets/image/yh2.png"
import seckill from "../assets/image/seckill.png"
import top from "../assets/image/top.png"
import brand from "../assets/image/brand.png"
import selfsupport from "../assets/image/selfsupport.png"
import integral from "../assets/image/integral.png"
import oneone from "../assets/image/11.png"
import cg1 from "../assets/image/cg1.png"
import cg2 from "../assets/image/cg2.png"

// 导入request
import { newS, wanRen, timeKill, indexGoods } from "../request/home_request"


export default class Home extends Component {
    state = {
        person_new: [], //新人专享
        wanren_team: [], //万人团
        time_kill: [],  //秒杀
        goods_list: [{
            description: '',
            goodsname: '',
            id: 1,
            img: '',
            market_price: 5.99,
            price: 2.99
        }], //商品部分
        good_index: 0 //面板的索引
    }
    get_goods(index) {
        indexGoods(index).then(res => {
            this.setState({ good_index: index })
            this.setState({ goods_list: res.list[index].content })
        })
    }
    UNSAFE_componentWillMount() {
        newS().then(res => {
            this.setState({ person_new: res.list })
        })
        wanRen().then(res => {
            this.setState({ wanren_team: res.list })
        })
        timeKill().then(res => {
            this.setState({ time_kill: res.list })
        })
        this.get_goods(0)
    }
    render() {
        const { person_new, wanren_team, time_kill, goods_list, good_index } = this.state
        return (
            <>
                <div className="container" id="home">
                    <div className="header">
                        <img src={logo} alt="" />
                        <form action="">
                            <input type="text" placeholder="搜索商品" />
                            <button>搜索</button>
                        </form>
                    </div>

                    <div className="middle">
                        <div className="xrzx">
                            <div className="clearfix top">
                                <h2 className="ft">新人专享</h2>
                                <a href="###" className="rt">查看全部&gt;</a>
                            </div>
                        </div>

                        <div className="xrzx1">
                            <p>
                                <span>1重礼</span>
                                <span>新人特价商品专区</span>
                                <span>（限量供应，先到先得）</span>
                            </p>

                            <div className="look">
                                <ul>
                                    {
                                        person_new.map(item => {
                                            return <li key={item.id}>
                                                <img src={'http://127.0.0.1:3001' + item.img} alt="" />
                                                <span className="now">￥{item.price}</span>
                                                <span className="pri">￥{item.market_price}</span>
                                                <a href="###"></a>
                                            </li>
                                        })
                                    }
                                </ul>
                            </div>

                        </div>
                        <div className="xrzx2">
                            <p>
                                <span>2重礼</span>
                                <span>新人通用50元礼券（下单立减，省了又省）</span>
                            </p>
                            <div>
                                <img className="yh1" src={yh} alt="" />
                                <img className="yh2" src={yh2} alt="" />
                            </div>
                        </div>
                    </div>
                    <div className="middle-nav box">
                        <ul>
                            <li>
                                <a href="###">
                                    <img src={seckill} alt="" />
                                    <p>限时秒杀</p>
                                </a>
                            </li>
                            <li>
                                <a href="###">
                                    <img src={top} alt="" />
                                    <p>畅销商品</p>
                                </a>
                            </li>
                            <li>
                                <a href="###">
                                    <img src={brand} alt="" />
                                    <p>品质大牌</p>
                                </a>
                            </li>
                            <li>
                                <a href="###">
                                    <img src={selfsupport} alt="" />
                                    <p>小u自营</p>
                                </a>
                            </li>
                            <li>
                                <a href="###">
                                    <img src={integral} alt="" />
                                    <p>积分商城</p>
                                </a>
                            </li>
                        </ul>
                    </div>
                    <div className="ms">
                        <div className="cjms">
                            <div className="top">
                                <h2 className="ft">万人团</h2>
                            </div>
                            {
                                wanren_team.map(item => (

                                    <div className="content" key={item.id}>
                                        <img src={this.$static_host + item.img} alt="" />
                                        <div>
                                            <span>7.9折</span>
                                            <p>{item.goodsname}......</p>
                                            <p><b>￥{item.price}</b><u>￥{item.market_price}</u></p>
                                        </div>
                                    </div>

                                ))
                            }
                        </div>


                        <div className="xsms">
                            <p>
                                <span>限时秒杀</span>
                                <span>新人专区</span>
                                <span>（限量供应）</span>
                            </p>
                            <div className="look">
                                <ul>
                                    {
                                        time_kill.map(item => {
                                            return <li key={item.id}>
                                                <img src={'http://127.0.0.1:3001' + item.img} alt="" />
                                                <span className="now">￥{item.price}</span>
                                                <span className="pri">￥{item.market_price}</span>
                                                <a href="###"></a>
                                            </li>
                                        })
                                    }

                                </ul>
                            </div>
                        </div>
                    </div>
                    <div className="two11">
                        <div className="one">
                            <h3>双11尖货预约</h3>
                            <img src={oneone} alt="" />
                        </div>
                        <div className="three">
                            <h3>畅购全球</h3>
                            <img src={cg1} alt="" />
                            <img src={cg2} alt="" />
                        </div>
                    </div>

                    <div className='nav'>
                        <ul>
                            <li className={good_index === 0 ? "active" : ''} onClick={() => { this.get_goods(0) }}>
                                <p>热销好货</p>
                                <span>限量抢购</span>
                            </li>
                            <li className={good_index === 1 ? "active" : ''} onClick={() => { this.get_goods(1) }}>
                                <p>折上折区</p>
                                <span>不买就亏</span>
                            </li>
                            <li className={good_index === 2 ? "active" : ''} onClick={() => { this.get_goods(2) }}>
                                <p>时令水果</p>
                                <span>当季限定</span>
                            </li>
                            <li className={good_index === 3 ? "active" : ''} onClick={() => { this.get_goods(3) }}>
                                <p>粮油调味</p>
                                <span>一键购齐</span>
                            </li>
                        </ul>
                    </div>
                    <div className="main">
                        {
                            goods_list.map(item => {
                                return <div className="item clearfix" key={item.id}>
                                    <div className="pic ft">
                                        <img src={this.$static_host + item.img} alt="" />
                                    </div>
                                    <div className="intro ft">
                                        <span>{(item.price/item.market_price*10).toFixed(1)}折</span>
                                        <p>{item.goodsname}</p>

                                        <u>￥{item.price}</u>
                                        <u>￥{item.market_price}</u>
                                        <button>加入购物车</button>
                                    </div>
                                </div>
                            })
                        }
                    </div>
                </div>
            </>
        )
    }
}
